package com.xdf.xzymanagementsystem.service.impl;

import com.xdf.xzymanagementsystem.entity.SysUser;
import com.xdf.xzymanagementsystem.service.SysParamsService;
import com.xdf.xzymanagementsystem.service.SysUserService;
import com.xdf.xzymanagementsystem.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class SysParamsServiceImpl extends BaseService implements SysParamsService {
    @Autowired
    private SysUserService sysUserService;

    // 查询获取当前登录用户的 id
    @Override
    public Integer getLoginUserId() {
        return super.getLoginUserId();
    }

    // 返回当前登录系统的用户对象
    @Override
    public SysUser getLoginUser() {
        Integer userId = Optional.ofNullable(getLoginUserId()).orElse(0);
        if( userId <=0 )
            return null;

        SysUser sysUser = null;
        try {
            sysUser = sysUserService.selectByPrimaryKey(userId);
        } catch (Exception e) {
            throw new RuntimeException("根据主键" + userId + "查询用户时出现异常！");
        }
        return sysUser;
    }

    @Override
    public String getUserName() {
        SysUser sysUser = Optional.ofNullable(getLoginUser()).orElse(new SysUser());
        String userName = Optional.ofNullable(sysUser.getUserName()).orElse("");
        return userName;
    }
}
